package MapOrganising;

import java.util.Vector;

/**
 * Handles the creation and refreshing of lists of files for the server to send
 * to clients.
 * 
 * @author Lewis Marhin and John Gray-Spence
 */
public class ListMaker {

    private static boolean listMakerInitialised = false;
    
    // Stores a list of file and their catagories.
    private static Vector<ListObject> masterList = new Vector<ListObject>();

    /**
     * Intialises the database of maps that is stored in a file. Does not
     * reinitialise if it has already run once.
     * 
     * @return String containing success or no success message.
     */
    public static String startListMaker() {
        if( listMakerInitialised ) {
            return "Database already initialised.";
        } else {
            FileHandling.MasterOps.loadMaster(Server.Constants.MASTER_LIST_FILE);
            listMakerInitialised = true;
            return "Database successfully intialised.";
        }
    }
    
    /**
     * Stops server from keeping track of saved mind maps
     *
     * @return Confirmed message or Not Running message.
     */
    public static String stopListMaker() {
        if( listMakerInitialised ) {
            FileHandling.MasterOps.saveMaster( Server.Constants.MASTER_LIST_FILE, masterList.toString() );
            listMakerInitialised = false;
            return "Database stopped.";
        } else {
            return "Database is not running.";
        }
    }
    
    /**
     * Generates list of MAXIMUM_FILE_SEND number of files.
     *
     * @return list An array with size MAXIMUM_FILE_SEND containing files.
     */
    public static ListObject[] getRandomList() {
        ListObject[] list = null;
        return list;
    }

    /**
     * Returns the master list of maps on the server.
     *
     * @return String containing headings seperated by tabs followed by a mapname, catagories and rating on the next line (seperated by tabs).
     */
    public static String getMasterList() {
        if( masterList.size() > 0) {
            String tmp = "Name\tCatagories\t\tRating";

            for( int i = 0; i < masterList.size(); i++) {
                ListObject tmp2 = masterList.get(i);
                tmp += "\n" + tmp2.fname + "\t" + tmp2.getCat() + "\t\t" + tmp2.getRating();
            }
            return tmp;
        } else {
            return "No maps in server database";
        }
    }

    /**
     * Updates the master list of files that ListMaker searches through to
     * produce file lists.
     * 
     * @param fname Name of file to be added to the list.
     * @param map The actual mindmap.
     * @param cats Categories the file to pertains to, seperated with DELIMITER_CAT
     * @param rating The rating of the current map ( 1 to 5 ).
     */
    public static void updateMasterList( String fname, String map, String cats, String rating ) {
        masterList.add( new ListObject( fname, map, cats, rating ) );
        System.out.println( FileHandling.FileOps.newFile( fname, map ) );
    }
}
